
using System;
using System.Reflection;
using System.Collections.Generic;

namespace Gamadus.Builder
{
	/// <summary>
	/// Helper class, gets engine variables out of compiled assembly
	/// </summary>
	public class EngineMgr : Gamadus.Core.SingletonManager<EngineMgr>
	{
		Assembly assembly;
		List<string> variables = new List<string>();
		List<string> functions = new List<string>();
		
		public List<string> Variables
		{
			get
			{
				return variables;
			}
		}
		
		public List<string> Functions
		{
			get
			{
				return functions;
			}
		}
		
		List<string> objectVars = new List<string>();
		public List<string> ObjectVars
		{
			get
			{
				return objectVars;
			}
		}
		List<string> roomVars = new List<string>();
		public List<string> RoomVars
		{
			get
			{
				return roomVars;
			}
		}
		
		public void Init(string engineFile)
		{
			assembly = Assembly.LoadFile(engineFile);
			Type gml = assembly.GetType("Gamadus.Engine.Gml");
			Console.WriteLine("Loading Gml builtins out of type "+gml.FullName);
			foreach(PropertyInfo property in gml.GetProperties())
			{
				variables.Add(property.Name);
			}
			foreach(MethodInfo method in gml.GetMethods())
			{
				if(method.Name == "GetType" || method.Name == "ToString" || method.Name == "Equals" || method.Name == "GetHashCode")
					continue;
				
				if((method.Name.StartsWith("get_") || method.Name.StartsWith("set_"))
				   && variables.Contains(method.Name.Substring(4)))
					continue;
				
				functions.Add(method.Name);
			}
			foreach(FieldInfo field in gml.GetFields())
			{
				variables.Add(field.Name);
			}
			
			foreach(PropertyInfo prop in assembly.GetType("Gamadus.Engine.ObjectSuper").GetProperties())
			{
				objectVars.Add(prop.Name);
			}
			foreach(FieldInfo field in assembly.GetType("Gamadus.Engine.ObjectSuper").GetFields())
			{
				objectVars.Add(field.Name);
			}			
			
			foreach(PropertyInfo prop in assembly.GetType("Gamadus.Engine.Room").GetProperties())
			{
				roomVars.Add(prop.Name);
			}
			foreach(FieldInfo field in assembly.GetType("Gamadus.Engine.Room").GetFields())
			{
				roomVars.Add(field.Name);
			}	
		}
		
//		Assembly ass;
//		public List<string> BuiltinGlobals = new List<string>();
//		public Assembly Ass
//		{
//			get
//			{
//				return ass;
//			}
//			set
//			{
//				ass = value;
//				foreach(Module mod in ass.GetModules())
//				{
//					foreach(Type type in mod.GetTypes())
//					{
//						string name = type.FullName;
//						if(name == "GamadusEngine.GML+Builtin")
//						{
//							LoadBuiltinGlobals(type);
//						}
//					}
//				}
//			}
//		}
//		
//		void LoadBuiltinGlobals(Type type)
//		{
//			Console.WriteLine("Loading builtins out of "+type.FullName);
//			foreach(PropertyInfo prop in type.GetProperties())
//			{
//				BuiltinGlobals.Add(prop.Name);
//			}
//			foreach(FieldInfo field in type.GetFields())
//			{
//				BuiltinGlobals.Add(field.Name);
//			}
//		}
	}
}
